package helper

import (
	"math"
	"server/share/config"

	"github.com/hwcer/cosgo/slice"
)

func Formula(lv int32, a, b, c, d float32) float32 {
	r := float64(lv)
	if a > 1 {
		r = math.Floor(r / float64(a))
	}
	if b > 1 {
		r = math.Pow(r, float64(b))
	}
	r = r * float64(c)
	r += float64(d)
	return float32(r)
}

// lv 战区层数
func AddItemsFromFormula(lv int32, num int32, formulaStr string) map[int32]int32 {
	r := make(map[int32]int32)
	arr := slice.Multiple(formulaStr, ",", ":")
	for _, v := range arr {
		if len(v) >= 2 && v[0] > 0 && v[1] > 0 {
			if formula := config.Data.Formula[v[1]]; formula != nil && len(formula.Args) >= 4 {
				if n := Formula(lv, formula.Args[0], formula.Args[1], formula.Args[2], formula.Args[3]); n > 0 {
					r[v[0]] += int32(n) * num
				}
			}
		}
	}
	return r
}
